{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://mp.weixin.qq.com/s/Xx1EFDlDHKZ6csj7OjVO7w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、一行代码实现1--100之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(range(0,101))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、如何在一个函数内部修改全局变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "def fu():\n",
    "    global a\n",
    "    a = 9\n",
    "fu()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、列出5个python标准库\n",
    "\n",
    "os：提供了不少与操作系统相关联的函数\n",
    "\n",
    "sys:   通常用于命令行参数\n",
    "\n",
    "re:   正则匹配\n",
    "\n",
    "math: 数学运算\n",
    "\n",
    "datetime:处理日期时间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4、字典如何删除键(del)和合并(update)两个字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'age': 26}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic = {'name':'yxl','age':26}\n",
    "del dic['name']\n",
    "dic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'age': 26, 'name': 'ljt'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dic2 = {'name':'ljt'}\n",
    "dic.update(dic2)\n",
    "dic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5、谈下python的GIL\n",
    "\n",
    "GIL 是python的全局解释器锁，同一进程中假如有多个线程运行，一个线程在运行python程序的时候会霸占python解释器（加了一把锁即GIL），使该进程内的其他线程无法运行，等该线程运行完后其他线程才能运行。如果线程运行过程中遇到耗时操作，则解释器锁解开，使其他线程运行。所以在多线程中，线程的运行仍是有先后顺序的，并不是同时进行。\n",
    "多进程中因为每个进程都能被系统分配资源，相当于每个进程有了一个python解释器，所以多进程可以实现多个进程的同时运行，缺点是进程系统资源开销大\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6、python实现列表去重的方法\n",
    "先通过集合去重，在转列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{12, 13, 14, 15, 16, 17}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list = [16,12,13,12,13,14,15,17]\n",
    "a = set(list)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[12, 13, 14, 15, 16, 17]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x for x in a]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7、fun(*args,**kwargs)中的*args,**kwargs什么意思？\n",
    "当不定数量的参数传递给一个函数时可以使用*args与**kwargs(不定数量：预先不知道有多少个参数)\n",
    "*args是用来发送一个键值对可变数量的参数列表给一个函数，意思就是没有key值。（以列表和元组的形式传参数）\n",
    "**kwargs将不定长度的键值对，作为参数传递给一个函数。意思就是有key值。（传入字典）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "def demo(args_f,*args_v):\n",
    "    print (args_f)\n",
    "    for x in args_v:\n",
    "        print (x)\n",
    "demo('a','b','c','d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "def demo(args_f,*args_v):\n",
    "    print(args_f)\n",
    "    for x in args_v:\n",
    "        print(x)\n",
    "demo('a','b','c','d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "def demo(args_f,*args_v):\n",
    "    print(args_f)\n",
    "    for x in args_v:\n",
    "        print(x)\n",
    "demo('a','b','c','d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name ninc\n"
     ]
    }
   ],
   "source": [
    "def demo(**args_v):\n",
    "    for k,v in args_v.items():\n",
    "        print(k,v)\n",
    "demo(name = 'ninc')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8、python2和python3的range（100）的区别\n",
    "\n",
    "python2返回列表，python3返回迭代器，节约内存"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9、一句话解释什么样的语言能够用装饰器?\n",
    "\n",
    "函数可以作为参数传递的语言，可以使用装饰器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10、python内建数据类型有哪些\n",
    "\n",
    "整型--int\n",
    "\n",
    "布尔型--bool\n",
    "\n",
    "字符串--str‘’/“”不可变\n",
    "\n",
    "列表--list:[]可变\n",
    "\n",
    "元组--tuple:()不可变\n",
    "\n",
    "字典--dict:{}可变"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11、简述面向对象中__new__和__init__区别\n",
    "__init__是初始化方法，创建对象后，就立刻被默认调用了，可接收参数。\n",
    "\n",
    "1、__new__至少要有一个参数cls，代表当前类，此参数在实例化时由Python解释器自动识别\n",
    "\n",
    "2、__new__必须要有返回值，返回实例化出来的实例，这点在自己实现__new__时要特别注意，\n",
    "可以return父类（通过super(当前类名, cls)）__new__出来的实例，或者直接是object的__new__出来的实例\n",
    "\n",
    "3、__init__有一个参数self，就是这个__new__返回的实例，__init__在__new__的基础上可以完成一些其它初始化的动作，__init__不需要返回值\n",
    "\n",
    "4、如果__new__创建的是当前类的实例，会自动调用__init__函数，通过return语句里面调用的__new__函数的第一个参数是cls来保证是当前类实例，\n",
    "如果是其他类的类名，；那么实际创建返回的就是其他类的实例，其实就不会调用当前类的__init__函数，也不会调用其他类的__init__函数。\n",
    "\n",
    "注：init方法中的self和new方法返回值地址一样，说明返回值是对象\n",
    "    cls和类ID一样，说明指向同一个类，也就是cls就是创建的实例类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "车的颜色为：green\n",
      "车轮数量为：2\n"
     ]
    }
   ],
   "source": [
    "class Bike:\n",
    "    def __init__(self,newWheelNum,newColor):\n",
    "        self.wheelNum = newWheelNum\n",
    "        self.color = newColor\n",
    "        \n",
    "    def move(self):\n",
    "        print(\"车会跑\")\n",
    "#创建对象\n",
    "BM = Bike(2,'green')\n",
    "\n",
    "print('车的颜色为：%s'%BM.color)\n",
    "print('车轮数量为：%d'%BM.wheelNum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是cls的ID 2041249291288\n",
      "这是new方法 <__main__.A object at 0x000001DB45D6B400>\n",
      "这是init方法 <__main__.A object at 0x000001DB45D6B400>\n",
      "这是类A的ID 2041249291288\n"
     ]
    }
   ],
   "source": [
    "class A(object):\n",
    "    def __init__(self):\n",
    "        print(\"这是init方法\",self)\n",
    "        \n",
    "    def __new__(cls):\n",
    "        print(\"这是cls的ID\",id(cls))\n",
    "        print(\"这是new方法\",object.__new__(cls))\n",
    "        return object.__new__(cls)\n",
    "A()\n",
    "print(\"这是类A的ID\",id(A))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12、简述with方法打开处理文件帮我我们做了什么？\n",
    "打开文件在进行读写的时候可能会出现一些异常状况，如果按照常规的f.open写法，\n",
    "我们需要try,except,finally，做异常判断，并且文件最终不管遇到什么情况，都要执行finally f.close()关闭文件，with方法帮我们实现了finally中f.close\n",
    "（当然还有其他自定义功能，有兴趣可以研究with方法源码）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open(\"./1.txt\",'wb')\n",
    "try:\n",
    "    f.write(\"hello world\")\n",
    "except:\n",
    "    pass\n",
    "finally:\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13、列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25]，并使用列表推导式提取出大于10的数，最终输出[16,25]\n",
    "map（）函数第一个参数是fun，第二个参数是一般是list，第三个参数可以写list，也可以不写，根据需求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[16, 25]\n"
     ]
    }
   ],
   "source": [
    "list = [1,2,3,4,5]\n",
    "def fn(x):\n",
    "    return x**2\n",
    "\n",
    "res = map(fn,list)\n",
    "res = [i for i in res if i >10]\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14、python中生成随机整数、随机小数、0--1之间小数方法\n",
    "\n",
    "随机整数：random.randint(a,b),生成区间内的整数\n",
    "\n",
    "随机小数：习惯用numpy库，利用np.random.randn(5)生成5个随机小数\n",
    "\n",
    "0-1随机小数：random.random(),括号中不传参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正整数 76\n",
      "5个随机小数 [-0.35666758 -0.77882166 -0.11057942  0.52242526  0.71192184]\n",
      "0-1随机小数 0.40256176681658384\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import numpy as np\n",
    "result = random.randint(1,90)\n",
    "res = np.random.randn(5)\n",
    "ret = random.random()\n",
    "\n",
    "print(\"正整数\",result)\n",
    "print(\"5个随机小数\",res)\n",
    "print(\"0-1随机小数\",ret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 15、避免转义给字符串加哪个字母表示原始字符串？\n",
    "\n",
    "r , 表示需要原始字符串，不转义特殊字符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 16、 <div class=\"nam\">中国</div>，用正则匹配出标签里面的内容（“中国”），其中class的类名是不确定的\n",
    "\n",
    "注：. 表示可有可无\n",
    "    * 表示任意字符，满足类名可以变化\n",
    "    (.*?)提取文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['中国']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "str = '<div class=\"nam\">中国</div>'\n",
    "res = re.findall(r'<div class=\".*\">(.*?)</div>',str)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 17、python中断言方法举例\n",
    "assert（）方法，断言成功，则程序继续执行，断言失败，则程序报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "断言成功，程序继续执行\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-1d587068d127>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m9\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m<\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"断言失败，程序报错\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "assert(a>1)\n",
    "print(\"断言成功，程序继续执行\")\n",
    "\n",
    "b = 9\n",
    "assert(b<3)\n",
    "print(\"断言失败，程序报错\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 18、数据表student有id,name,score,city字段，其中name中的名字可有重复，需要消除重复行,请写sql语句\n",
    "\n",
    "select  distinct  name  from  student"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 19、10个Linux常用命令\n",
    "\n",
    "ls：查看文件信息\n",
    "\n",
    "pwd：显示当前路径\n",
    "\n",
    "cd：切换工作目录\n",
    "\n",
    "clear：清屏\n",
    "\n",
    "touch：创建文件\n",
    "\n",
    "rm：删除文件\n",
    "\n",
    "mkdir：创建目录\n",
    "\n",
    "tree：以树状形式显示目录结构\n",
    "\n",
    "cp：拷贝\n",
    "\n",
    "mv：重命名、移动\n",
    "\n",
    "cat：显示文件内容\n",
    "\n",
    "more：以全屏幕的方式按页来显示文本文件的内容\n",
    "\n",
    "grep：文本搜索,grep+[options]+要搜索的字符串或正则表达式+filename\n",
    "\n",
    "echo:在显示器上显示一段文字，一般起到一个提示的作用,echo [ -n ] 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 20、python2和python3区别？列举5个\n",
    "\n",
    "1、Python3 使用 print 必须要以小括号包裹打印内容，比如 print('hi');Python2 既可以使用带小括号的方式，也可以使用一个空格来分隔打印内容，比如 print 'hi'\n",
    "\n",
    "2、python2 range(1,10)返回列表，python3中返回迭代器，节约内存\n",
    "\n",
    "3、python2中使用ascii编码，python中使用utf-8编码\n",
    "\n",
    "4、python2中unicode表示字符串序列，str表示字节序列; python3中str表示字符串序列，byte表示字节序列\n",
    "\n",
    "5、python2中为正常显示中文，引入coding声明，python3中不需要\n",
    "\n",
    "6、python2中是raw_input()函数，python3中是input()函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 21、列出python中可变数据类型和不可变数据类型，并简述原理\n",
    "\n",
    "不可变数据类型：数值型、字符串型string和元组tuple\n",
    "\n",
    "不允许变量的值发生变化，如果改变了变量的值，相当于是新建了一个对象，而对于相同的值的对象，在内存中则只有一个对象（一个地址），如下图用id()方法可以打印对象的id\n",
    "\n",
    "可变数据类型：列表list和字典dict；\n",
    "\n",
    "允许变量的值发生变化，即如果对变量进行append、+=等这种操作后，只是改变了变量的值，而不会新建一个对象，变量引用的对象的地址也不会变化，不过对于相同的值的不同对象，在内存中则会存在不同的对象，即每个对象都有自己的地址，相当于内存中对于同值的对象保存了多份，这里不存在引用计数，是实实在在的对象。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1483583840"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = 5\n",
    "b = 5\n",
    "id(a)\n",
    "id(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1483583840"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1483583840"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 22、s = \"ajldjlajfdljfddd\"，去重并从小到大排序输出\"adfjl\"\n",
    "\n",
    "set去重，去重转成list,利用sort方法排序，reverse=False是从小到大排\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adfjl\n"
     ]
    }
   ],
   "source": [
    "s = \"ajldjlajfdljfddd\"\n",
    "s = set(s)\n",
    "s = list(s)\n",
    "s.sort(reverse = False)\n",
    "res = \"\".join(s)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 23、用lambda函数实现两个数相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "sum = lambda a,b:a*b\n",
    "print(sum(5,4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 24、字典根据键从小到大排序\n",
    "\n",
    "dict={\"name\":\"zs\",\"age\":18,\"city\":\"深圳\",\"tel\":\"1362626627\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sorted根据字典键排序 [('age', 18), ('city', '深圳'), ('name', 'zs'), ('tel', '1362626627')]\n",
      "新字典 {'age': 18, 'city': '深圳', 'name': 'zs', 'tel': '1362626627'}\n"
     ]
    }
   ],
   "source": [
    "dict={\"name\":\"zs\",\"age\":18,\"city\":\"深圳\",\"tel\":\"1362626627\"}\n",
    "list = sorted(dict.items(),key = lambda i:i[0],reverse=False)\n",
    "print(\"sorted根据字典键排序\",list)\n",
    "new_dict = {}\n",
    "for i in list:\n",
    "    new_dict[i[0]] = i[1]\n",
    "print(\"新字典\",new_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 25、利用collections库的Counter方法统计字符串每个单词出现的次数\"kjalfj;ldsjafl;hdsllfdhg;lahfbl;hl;ahlf;h\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({'l': 9, ';': 6, 'h': 6, 'f': 5, 'a': 4, 'j': 3, 'd': 3, 's': 2, 'k': 1, 'g': 1, 'b': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "a = \"kjalfj;ldsjafl;hdsllfdhg;lahfbl;hl;ahlf;h\"\n",
    "res = Counter(a)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 26、字符串a = \"not 404 found 张三 99 深圳\"，每个词中间是空格，用正则过滤掉英文和数字，最终输出\"张三  深圳\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['not', '404', 'found', '张三', '99', '深圳']\n",
      "['not', '404', 'found', '99']\n",
      "张三 深圳\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "a =  \"not 404 found 张三 99 深圳\"\n",
    "list = a.split(\" \")\n",
    "print(list)\n",
    "res = re.findall('\\d+|[a-zA-Z]+',a)\n",
    "for i in res:\n",
    "    if i in list:\n",
    "        list.remove(i)\n",
    "new_str = \" \".join(list)\n",
    "print(res)\n",
    "print(new_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 27、filter方法求出列表所有奇数并构造新列表，a =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "filter() 函数用于过滤序列，过滤掉不符合条件的元素，返回由符合条件元素组成的新列表。该接收两个参数，第一个为函数，第二个为序列，序列的每个元素作为参数传递给函数进行判，然后返回 True 或 False，最后将返回 True 的元素放到新列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "a =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "def fn(a):\n",
    "    return a%2 == 1\n",
    "newlist = filter(fn,a)\n",
    "newlist = [i for i in newlist]\n",
    "print(newlist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 28、列表推导式求列表所有奇数并构造新列表，a =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "res = [i for i in a if i%2 == 1]\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 29、正则re.complie作用\n",
    "\n",
    "re.compile是将正则表达式编译成一个对象，加快速度，并重复使用\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 30、a=（1，）b=(1)，c=(\"1\") 分别是什么类型的数据？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((\"1\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type((1,))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 31、两个列表[1,5,7,9]和[2,2,6,8]合并为[1,2,2,5,6,7,8,9]\n",
    "\n",
    "extend可以将另一个集合中的元素逐一添加到列表中，区别于append整体添加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 5, 7, 9, 2, 2, 6, 8]\n",
      "[1, 2, 2, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "list1 = [1,5,7,9]\n",
    "list2 = [2,2,6,8]\n",
    "list1.extend(list2)\n",
    "print(list1)\n",
    "list1.sort(reverse = False)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 32、用python删除文件和用linux命令删除文件方法\n",
    "\n",
    "python：os.remove(文件名)\n",
    "\n",
    "linux:       rm  文件名\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 33、log日志中，我们需要用时间戳记录error,warning等的发生时间，请用datetime模块打印当前时间戳 “2018-04-01 11:38:54”\n",
    "\n",
    "顺便把星期的代码也贴上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-07-20 15:44:366\n"
     ]
    }
   ],
   "source": [
    "import datetime\n",
    "a = str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))+str(datetime.datetime.now().isoweekday())\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 34、数据库优化查询方法\n",
    "\n",
    "外键、索引、联合查询、选择特定字段等等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 35、请列出你会的任意一种统计图（条形图、折线图等）绘制的开源库，第三方也行\n",
    "\n",
    "pychart、matplotlib、plotly"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 36、写一段自定义异常代码\n",
    "\n",
    "自定义异常用raise抛出异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字大于2了\n"
     ]
    }
   ],
   "source": [
    "def fn():\n",
    "    try:\n",
    "        for i in range(5):\n",
    "            if i>2:\n",
    "                raise Exception(\"数字大于2了\")\n",
    "    except Exception as ret:\n",
    "        print(ret)\n",
    "fn()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 37、正则表达式匹配中，（.*）和（.*?）匹配区别？\n",
    "\n",
    "（.*）是贪婪匹配，会把满足正则的尽可能多的往后匹配\n",
    "\n",
    "（.*?）是非贪婪匹配，会把满足正则的尽可能少匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "贪婪匹配 ['哈哈</a><a>呵呵']\n",
      "非贪婪匹配 ['哈哈', '呵呵']\n"
     ]
    }
   ],
   "source": [
    "s = \"<a>哈哈</a><a>呵呵</a>\"\n",
    "import re\n",
    "res1 = re.findall(\"<a>(.*)</a>\",s)\n",
    "print(\"贪婪匹配\",res1)\n",
    "res2 = re.findall(\"<a>(.*?)</a>\",s)\n",
    "print(\"非贪婪匹配\",res2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 38、简述Django的orm\n",
    "\n",
    "ORM，全拼Object-Relation Mapping，意为对象-关系映射\n",
    "\n",
    "实现了数据模型与数据库的解耦，通过简单的配置就可以轻松更换数据库，而不需要修改代码只需要面向对象编程,orm操作本质上会根据对接的数据库引擎，翻译成对应的sql语句,所有使用Django开发的项目无需关心程序底层使用的是MySQL、Oracle、sqlite....，如果数据库迁移，只需要更换Django的数据库引擎即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 39、[[1,2],[3,4],[5,6]]一行代码展开该列表，得出[1,2,3,4,5,6]\n",
    "运行过程：for i in a ,每个i是【1,2】，【3,4】，【5,6】，for j in i，每个j就是1,2,3,4,5,6,合并后就是结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "a = [[1,2],[3,4],[5,6]]\n",
    "x = [j for i in a for j in i]\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "b = np.array(a).flatten().tolist()\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python3 正则表达式\n",
    "https://www.runoob.com/python3/python3-reg-expressions.html\n",
    "\n",
    "https://www.runoob.com/regexp/regexp-metachar.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "\\w\t 匹配字母、数字、下划线。等价于'[A-Za-z0-9_]'。\n",
    "\n",
    "\\W\t匹配非字母、数字、下划线。等价于 '[^A-Za-z0-9_]'。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svd\n"
     ]
    }
   ],
   "source": [
    "print(re.match(\"\\w{1,3}\", \"svdbshkv45646在\", flags=2).group())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 3)\n",
      "None\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(re.match('www', 'www.runoob.com').span())  # 在起始位置匹配\n",
    "print(re.match('com', 'www.runoob.com'))         # 不在起始位置匹配\n",
    "print(re.match('com', 'www.runoob.com'))         # 不在起始位置匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matchObj.group() :  Cats are smarter than dogs\n",
      "matchObj.group(1) :  Cats\n",
      "matchObj.group(2) :  smarter\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "no such group",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-43-52349afc9a7d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      7\u001b[0m    \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"matchObj.group(1) : \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmatchObj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m    \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"matchObj.group(2) : \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmatchObj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m    \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"matchObj.group(3) : \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmatchObj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     10\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m    \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"No match!!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: no such group"
     ]
    }
   ],
   "source": [
    "line = \"Cats are smarter than dogs\"\n",
    "# .* 表示任意匹配除换行符（\\n、\\r）之外的任何单个或多个字符\n",
    "matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I)\n",
    " \n",
    "if matchObj:\n",
    "   print (\"matchObj.group() : \", matchObj.group())\n",
    "   print (\"matchObj.group(1) : \", matchObj.group(1))\n",
    "   print (\"matchObj.group(2) : \", matchObj.group(2))\n",
    "   print (\"matchObj.group(3) : \", matchObj.group(3))\n",
    "else:\n",
    "   print (\"No match!!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "searchObj.group() :  Cats are smarter than dogs\n",
      "searchObj.group(1) :  Cats\n",
      "searchObj.group(2) :  smarter\n"
     ]
    }
   ],
   "source": [
    "# re.search方法\n",
    "#!/usr/bin/python3\n",
    " \n",
    "import re\n",
    " \n",
    "line = \"Cats are smarter than dogs\";\n",
    " \n",
    "searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I)\n",
    " \n",
    "if searchObj:\n",
    "   print (\"searchObj.group() : \", searchObj.group())\n",
    "   print (\"searchObj.group(1) : \", searchObj.group(1))\n",
    "   print (\"searchObj.group(2) : \", searchObj.group(2))\n",
    "else:\n",
    "   print (\"Nothing found!!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 检索和替换\n",
    "Python 的re模块提供了re.sub用于替换字符串中的匹配项。\n",
    "\n",
    "语法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "电话号码 :  2004-959-559 \n",
      "电话号码 :  2004959559\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python3\n",
    "import re\n",
    " \n",
    "phone = \"2004-959-559 # 这是一个电话号码\"\n",
    " \n",
    "# 删除注释\n",
    "num = re.sub(r'#.*$', \"\", phone)\n",
    "print (\"电话号码 : \", num)\n",
    " \n",
    "# 移除非数字的内容\n",
    "num = re.sub(r'\\D', \"\", phone)\n",
    "print (\"电话号码 : \", num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# repl 参数是一个函数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# compile 函数"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "\n",
    "compile 函数用于编译正则表达式，生成一个正则表达式（ Pattern ）对象，供 match() 和 search() 这两个函数使用。\n",
    "\n",
    "语法格式为：\n",
    "\n",
    "re.compile(pattern[, flags])\n",
    "参数：\n",
    "\n",
    "pattern : 一个字符串形式的正则表达式\n",
    "flags 可选，表示匹配模式，比如忽略大小写，多行模式等，具体参数为：\n",
    "re.I 忽略大小写\n",
    "re.L 表示特殊字符集 \\w, \\W, \\b, \\B, \\s, \\S 依赖于当前环境\n",
    "re.M 多行模式\n",
    "re.S 即为' . '并且包括换行符在内的任意字符（' . '不包括换行符）\n",
    "re.U 表示特殊字符集 \\w, \\W, \\b, \\B, \\d, \\D, \\s, \\S 依赖于 Unicode 字符属性数据库\n",
    "re.X 为了增加可读性，忽略空格和' # '后面的注释\n",
    "实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "pattern = re.compile(r'\\d+') \n",
    "m = pattern.match('one12twothree34four') \n",
    "m   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 40、x=\"abc\",y=\"def\",z=[\"d\",\"e\",\"f\"],分别求出x.join(y)和x.join(z)返回的结果\n",
    "\n",
    "join()括号里面的是可迭代对象，x插入可迭代对象中间，形成字符串，结果一致，"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dabceabcf\n",
      "dabceabcf\n"
     ]
    }
   ],
   "source": [
    "x=\"abc\"\n",
    "y=\"def\"\n",
    "z=[\"d\",\"e\",\"f\"]\n",
    "a = x.join(y)\n",
    "b = x.join(z)\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 41、举例说明异常模块中try except else finally的相关意义\n",
    "\n",
    "try..except..else没有捕获到异常，执行else语句\n",
    "\n",
    "try..except..finally不管是否捕获到异常，都执行finally语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "没有捕捉到异常，则执行该语句\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    num = 100\n",
    "    print(num)\n",
    "except NameError as errorMsg:\n",
    "    print(\"产生错误了：%s\"%errorMsg)\n",
    "else:\n",
    "    print(\"没有捕捉到异常，则执行该语句\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "不管是否捕获到异常，都执行该语句\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    num = 100\n",
    "    print(num)\n",
    "except NameError as errorMsg:\n",
    "    print(\"产生错误了：%s\"%errorMsg)\n",
    "finally:\n",
    "    print(\"不管是否捕获到异常，都执行该语句\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 42、python中交换两个数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 4\n",
      "4 3\n"
     ]
    }
   ],
   "source": [
    "a,b = 3,4\n",
    "print(a,b)\n",
    "a,b = b,a\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 43、举例说明zip（）函数用法\n",
    "\n",
    "zip()函数在运算时，会以一个或多个序列（可迭代对象）做为参数，返回一个元组的列表。同时将这些序列中并排的元素配对。\n",
    "\n",
    "zip()参数可以接受任何类型的序列，同时也可以有两个以上的参数;当传入参数的长度不同时，zip能自动以最短序列长度为准进行截取，获得元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 3), (2, 4)]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2]\n",
    "b = [3,4]\n",
    "res = [i for i in zip(a,b)]#传入列表\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 3), (2, 4)]\n"
     ]
    }
   ],
   "source": [
    "a = (1,2)\n",
    "b = (3,4)\n",
    "res = [i for i in zip(a,b)]#传入元组\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('a', 'x'), ('b', 'y')]\n"
     ]
    }
   ],
   "source": [
    "a = \"ab\"\n",
    "b = \"xyz\"\n",
    "res = [i for i in zip(a,b)]#传入字符串\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 44、a=\"张明 98分\"，用re.sub，将98替换为100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张明 100分\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "a = \"张明 98分\"\n",
    "ret = re.sub(r\"\\d+\",\"100\",a)\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 45、写5条常用sql语句\n",
    "\n",
    "show databases;查看数据库\n",
    "\n",
    "show tables;查看表\n",
    "\n",
    "desc 表名;查看表结构\n",
    "\n",
    "select * from 表名;查找表内容\n",
    "\n",
    "delete from 表名 where id=5;删除id为5的行\n",
    "\n",
    "update students set gender=0,hometown=\"北京\" where id=5 更新表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 46、a=\"hello\"和b=\"你好\"编码成bytes类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=\"hello\"\n",
    "b=\"你好\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 47、[1,2,3]+[4,5,6]的结果是多少？\n",
    "\n",
    "两个列表相加，等价于extend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a = [1,2,3]\n",
    "b = [4,5,6]\n",
    "res = a+b\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 48、提高python运行效率的方法\n",
    "\n",
    "1、使用生成器，因为可以节约大量内存\n",
    "\n",
    "2、循环代码优化，避免过多重复代码的执行\n",
    "\n",
    "3、核心模块用Cython  PyPy等，提高效率\n",
    "\n",
    "4、多进程、多线程、协程\n",
    "\n",
    "5、多个if elif条件判断，可以把最有可能先发生的条件放到前面写，这样可以减少程序判断的次数，提高效率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 49、简述mysql和redis区别\n",
    "\n",
    "redis： 内存型非关系数据库，数据保存在内存中，速度快\n",
    "\n",
    "mysql：关系型数据库，数据保存在磁盘中，检索的话，会有一定的Io操作，访问速度相对慢\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 50、遇到bug如何处理\n",
    "\n",
    "1、细节上的错误，通过print（）打印，能执行到print（）说明一般上面的代码没有问题，分段检测程序是否有问题，如果是js的话可以alert或console.log\n",
    "\n",
    "2、如果涉及一些第三方框架，会去查官方文档或者一些技术博客。\n",
    "\n",
    "3、对于bug的管理与归类总结，一般测试将测试出的bug用teambin等bug管理工具进行记录，然后我们会一条一条进行修改，修改的过程也是理解业务逻辑和提高自己编程逻辑缜密性的方法，我也都会收藏做一些笔记记录。\n",
    "\n",
    "4、导包问题、城市定位多音字造成的显示错误问题\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 51、正则匹配，匹配日期2018-03-20\n",
    "\n",
    "url='https://sycm.taobao.com/bda/tradinganaly/overview/get_summary.json?dateRange=2018-03-20%7C2018-03-20&dateType=recent1&device=1&token=ff25b109b&_=1521595613462'\n",
    "\n",
    "仍有同学问正则，其实匹配并不难，提取一段特征语句，用（.*?）匹配即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-59-523a19f9a241>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-59-523a19f9a241>\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m    result = re.findall(r.'dateRange=(.*?)%7c(.*?)', url)\u001b[0m\n\u001b[1;37m                                                  ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "url='https://sycm.taobao.com/bda/tradinganaly/overview/get_summary.json?dateRange=2018-03-20%7C2018-03-20&dateType=recent1&device=1&token=ff25b109b&_=1521595613462'\n",
    "result = re.findall(r.'dateRange=(.*?)%7c(.*?)', url)\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 52、list=[2,3,5,4,9,6]，从小到大排序，不许用sort，输出[2,3,4,5,6,9]\n",
    "\n",
    "利用min()方法求出最小值，原列表删除最小值，新列表加入最小值，递归调用获取最小值的函数，反复操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4, 5, 6, 9]\n"
     ]
    }
   ],
   "source": [
    "list=[2,3,5,4,9,6]\n",
    "new_list = []\n",
    "def get_min(list):\n",
    "    a = min(list)\n",
    "    list.remove(a)\n",
    "    new_list.append(a)\n",
    "    if len(list)>0:\n",
    "        get_min(list)\n",
    "        return new_list\n",
    "new_list = get_min(list)\n",
    "print(new_list)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 53、写一个单列模式\n",
    "\n",
    "因为创建对象时__new__方法执行，并且必须return 返回实例化出来的对象所cls.__instance是否存在，不存在的话就创建对象，存在的话就返回该对象，来保证只有一个实例对象存在（单列），打印ID，值一样，说明对象同一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 54、保留两位小数\n",
    "\n",
    "题目本身只有a=\"%.03f\"%1.3335,让计算a的结果，为了扩充保留小数的思路，提供round方法（数值，保留位数）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.333 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "a=\"%.03f\"%1.3335\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
